//
//  RootViewController.m
//  HelloList2
//
//  Created for CSE494
//

#import "RootViewController.h"
#import "ShoppingList.h"
#import "NoteViewController.h"

@implementation RootViewController
@synthesize addScreenVC;
@synthesize label;
@synthesize textField;
@synthesize theList;
@synthesize controllers;
@synthesize myList2;
@synthesize textView;
@synthesize noteButton;
@synthesize titleButton;

#pragma mark -
#pragma mark View lifecycle

- (IBAction)textFieldDoneEditing:(id)sender{
	[sender resignFirstResponder];
	[textField resignFirstResponder];
}

- (void)viewDidLoad {
		
	myList2 = [ShoppingList alloc];
	[myList2 init];
	
	self.theList = myList2;
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
																							target:self 
																							action:@selector(actionHappened)];
	[myList2 release];
	
	[super viewDidLoad];
}

- (void)actionHappened {
	
	[self.navigationController pushViewController:addScreenVC animated:YES];
}

- (IBAction)addPressed {

	textField.hidden=YES;
	titleButton.hidden=YES;
	
	textView.hidden = NO;
	noteButton.hidden = NO;
}

- (IBAction)addNotePressed {
	
	NSString *logString = textField.text;
	
	[myList2 addItem:logString];
	
	[self.navigationController popViewControllerAnimated:YES];
	
	textField.text = nil;
	textField.hidden= NO;
	titleButton.hidden=NO;
	textView.hidden = YES;
	noteButton.hidden = YES;
	textView.text = nil;

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	[self.tableView reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myList2 size];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	
	cell.textLabel.text = [myList2.myList objectAtIndex:indexPath.row];
	
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /*NSUInteger row = [indexPath row];
    NoteViewController *nextController = [self.controllers objectAtIndex:row];
    
    [self.navigationController pushViewController:nextController animated:YES];	
	*/
	
    if (childController == nil)
        childController = [[NoteViewController alloc] 
                           initWithNibName:@"NoteViewController" bundle:nil];
    
    childController.title = @"Note Viewer";
    NSUInteger row = [indexPath row];
    
    NSString *noteTitle = [myList2.myList objectAtIndex:row];
	
	childController.title = noteTitle;
	
	childController.titleLabel.text = noteTitle;
	childController.note = [myList2.noteList objectAtIndex:row];

	
    [self.navigationController pushViewController:childController
										 animated:YES];	
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

